package com.example.tianxi.yuexiuhhpatient.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.example.tianxi.yuexiuhhpatient.Adapter.HistoryAdapter;
import com.example.tianxi.yuexiuhhpatient.EditHistoryInfoActivity;
import com.example.tianxi.yuexiuhhpatient.Entity.HistoryEntity;
import com.example.tianxi.yuexiuhhpatient.HistoryInfoActivity;
import com.example.tianxi.yuexiuhhpatient.R;
import com.example.tianxi.yuexiuhhpatient.fragment.dummy.DummyContent;

import java.util.ArrayList;
import java.util.List;

/**
 * A fragment representing a list of Items.
 * <p/>
 * Large screen devices (such as tablets) are supported by replacing the ListView
 * with a GridView.
 * <p/>
 * Activities containing this fragment MUST implement the {@link OnFragmentInteractionListener}
 * interface.
 */
public class HistoryFragment extends Fragment implements AbsListView.OnItemClickListener {


    public static final String TAG = "HistoryFragment";

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

//    private OnFragmentInteractionListener mListener;

    /**
     * The fragment's ListView/GridView.
     */
    private AbsListView mListView;

    /**
     * The Adapter which will be used to populate the ListView/GridView with
     * Views.
     */
    private ListAdapter mAdapter;

    private List<HistoryEntity> lists;

    private View view;

    //新增病历按钮
    private TextView add;

    public static HistoryFragment newInstance() {
        HistoryFragment fragment = new HistoryFragment();
        return fragment;
    }

    // TODO: Rename and change types of parameters
    public static HistoryFragment newInstance(String param1, String param2) {
        HistoryFragment fragment = new HistoryFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public HistoryFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

        // TODO: Change Adapter to display your content
        //mAdapter = new ArrayAdapter<DummyContent.DummyItem>(getActivity(),android.R.layout.simple_list_item_1, android.R.id.text1, DummyContent.ITEMS);

        lists = new ArrayList<HistoryEntity>();

        lists.add(new HistoryEntity("王小波","男",45,"昨天22:18","内科","肚子疼","肠炎，外加轻微的胃炎，小肠气","偶尔发病"));
        lists.add(new HistoryEntity("韩寒","男",45,"昨天22:18","内科","肚子疼","肠炎，外加轻微的胃炎，小肠气","偶尔发病"));
        lists.add(new HistoryEntity("冯唐","男",45,"昨天22:18","内科","肚子疼","肠炎，外加轻微的胃炎，小肠气","偶尔发病"));
        lists.add(new HistoryEntity("莫言","男",45,"昨天22:18","内科","肚子疼","肠炎，外加轻微的胃炎，小肠气","偶尔发病"));
        lists.add(new HistoryEntity("村上春树","男",45,"昨天22:18","内科","肚子疼","肠炎，外加轻微的胃炎，小肠气","偶尔发病"));
        lists.add(new HistoryEntity("安妮宝贝","男",45,"昨天22:18","内科","肚子疼","肠炎，外加轻微的胃炎，小肠气","偶尔发病"));
        lists.add(new HistoryEntity("路金波","男",45,"昨天22:18","内科","肚子疼","肠炎，外加轻微的胃炎，小肠气","偶尔发病"));
        lists.add(new HistoryEntity("王朔","男",45,"昨天22:18","内科","肚子疼","肠炎，外加轻微的胃炎，小肠气","偶尔发病"));
        lists.add(new HistoryEntity("王安忆","男",45,"昨天22:18","内科","肚子疼","肠炎，外加轻微的胃炎，小肠气","偶尔发病"));
        //创建自己的适配器
        mAdapter = new HistoryAdapter(getActivity(),lists);


    }

    public void initView(){

        add = (TextView)view.findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), EditHistoryInfoActivity.class);
                intent.putExtra("flag",false);//设置true表示编辑，需要将页面数据传过去
                startActivity(intent);//这里应该使用startActivityForResult，带有编辑后的返回值，再改
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_item, container, false);

        // Set the adapter
        mListView = (AbsListView) view.findViewById(android.R.id.list);

//        ((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);

        mListView.setAdapter(mAdapter);
        // Set OnItemClickListener so we can be notified on item clicks
        mListView.setOnItemClickListener(this);

        initView();

        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
//            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
//        mListener = null;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        if (null != mListener) {
            // Notify the active callbacks interface (the activity, if the
            // fragment is attached to one) that an item has been selected.
//            mListener.onFragmentInteraction(DummyContent.ITEMS.get(position).id);
//        }
        //启动病例的详情页面
        Intent intent = new Intent(getActivity(), HistoryInfoActivity.class);
        intent.putExtra("textview1",lists.get(position).text1);
        intent.putExtra("textview2",lists.get(position).text2);
        intent.putExtra("textview3",lists.get(position).text3);
        intent.putExtra("textview4",lists.get(position).text4);
        startActivity(intent);
    }

    /**
     * The default content for this Fragment has a TextView that is shown when
     * the list is empty. If you would like to change the text, call this method
     * to supply the text it should use.
     */
    public void setEmptyText(CharSequence emptyText) {
        View emptyView = mListView.getEmptyView();

        if (emptyView instanceof TextView) {
            ((TextView) emptyView).setText(emptyText);
        }
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
//    public interface OnFragmentInteractionListener {
//        // TODO: Update argument type and name
//        public void onFragmentInteraction(String id);
//    }

}
